<template>
  <div class="statistics-container">
    <!-- 顶部导航栏 -->
    <div class="top-nav">
      <div class="title">借阅数据统计</div>
      <div class="controls">
        <div class="time-picker" v-if="showDatePicker">
          <el-date-picker
            v-model="timeRange"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            :shortcuts="dateShortcuts"
            @change="handleTimeRangeChange"
          />
        </div>
      </div>
    </div>

    <div class="main-content">
      <!-- 左侧菜单栏 -->
      <div class="left-menu">
        <div 
          v-for="(item, index) in menuItems" 
          :key="index"
          class="menu-item"
          :class="{ active: activeMenu === item.path }"
          @click="handleMenuClick(item.path)"
        >
          {{ item.label }}
        </div>
      </div>

      <!-- 右侧图表区域 -->
      <div class="right-content">
        <router-view v-slot="{ Component }">
          <keep-alive>
            <component :is="Component" :time-range="timeRange" />
          </keep-alive>
        </router-view>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, computed } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { ElMessage } from 'element-plus'

const router = useRouter()
const route = useRoute()
const timeRange = ref([])
const activeMenu = ref(route.path)

// 添加计算属性来控制日期选择器的显示
const showDatePicker = computed(() => {
  const showDatePickerPaths = [
    '/admin/statistics/inventory-status',
    '/admin/statistics/popular-books',
    '/admin/statistics/category-stats'
  ]
  return showDatePickerPaths.includes(route.path)
})

const menuItems = [
  { path: '/admin/statistics/dashboard', label: '综合数据看板' },
  { path: '/admin/statistics/borrowing-trend', label: '借阅趋势折线图' },
  { path: '/admin/statistics/category-stats', label: '图书分类饼图' },
  { path: '/admin/statistics/popular-books', label: '热门图书分析' },
  { path: '/admin/statistics/inventory-status', label: '库存状态仪表盘' },
  { path: '/admin/statistics/data-export', label: '数据导出' }
]

const dateShortcuts = [
  {
    text: '最近一周',
    value: () => {
      const end = new Date()
      const start = new Date()
      start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
      return [start, end]
    }
  },
  {
    text: '最近一个月',
    value: () => {
      const end = new Date()
      const start = new Date()
      start.setMonth(start.getMonth() - 1)
      return [start, end]
    }
  },
  {
    text: '最近三个月',
    value: () => {
      const end = new Date()
      const start = new Date()
      start.setMonth(start.getMonth() - 3)
      return [start, end]
    }
  }
]

const handleTimeRangeChange = (val) => {
  timeRange.value = val
}

const handleMenuClick = (path) => {
  activeMenu.value = path
  router.push(path)
}

onMounted(() => {
  // 如果当前路径不在菜单项中，默认跳转到第一个菜单
  if (!menuItems.some(item => item.path === route.path)) {
    handleMenuClick(menuItems[0].path)
  } else {
    activeMenu.value = route.path
  }
})
</script>

<style scoped>
.statistics-container {
  height: 100%;
  display: flex;
  flex-direction: column;
  background-color: #f5f7fa;
}

.top-nav {
  background-color: #fff;
  padding: 16px 24px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  box-shadow: 0 1px 4px rgba(0,21,41,0.08);
}

.controls {
  display: flex;
  align-items: center;
  gap: 16px;
}

.export-section {
  display: flex;
  align-items: center;
  gap: 8px;
}

.title {
  font-size: 20px;
  font-weight: 600;
  color: #303133;
}

.main-content {
  flex: 1;
  display: flex;
  overflow: hidden;
  margin: 16px;
  gap: 16px;
  min-height: 0;
}

.left-menu {
  width: 200px;
  background-color: #fff;
  border-radius: 4px;
  padding: 16px 0;
  overflow-y: auto;
}

.menu-item {
  padding: 12px 24px;
  color: #606266;
  cursor: pointer;
  font-size: 14px;
  transition: all 0.3s;
}

.menu-item:hover {
  color: #409EFF;
  background-color: #ecf5ff;
}

.menu-item.active {
  color: #409EFF;
  background-color: #ecf5ff;
  border-right: 2px solid #409EFF;
}

.right-content {
  flex: 1;
  overflow: hidden;
  background-color: #fff;
  border-radius: 4px;
  display: flex;
  flex-direction: column;
}

@media screen and (max-width: 1200px) {
  .controls {
    flex-direction: column;
    align-items: flex-end;
  }
  
  .export-section {
    margin-bottom: 8px;
  }
}

@media screen and (max-width: 768px) {
  .top-nav {
    flex-direction: column;
    gap: 16px;
  }
  
  .controls {
    width: 100%;
    align-items: stretch;
  }
  
  .export-section {
    flex-direction: column;
  }
  
  .time-picker {
    width: 100%;
  }
  
  :deep(.el-select) {
    width: 100% !important;
  }
  
  :deep(.el-date-editor) {
    width: 100% !important;
  }
}
</style> 